package org.tp8.sampleapp.jms.messagecreator;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.context.annotation.Scope;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
import org.tp8.sampleapp.model.BaseModel;
import org.tp8.sampleapp.util.SampleAppLogger;

/**
 * The Class MessageCreatorImpl.
 * 
 * @param <T>
 *            the generic type
 */
@Component
@Scope(value = "prototype")
public class MessageCreatorImpl<T extends BaseModel<Long>> implements
		MessageCreator {

	/**
	 * The Constant LOG.
	 */
	private static final SampleAppLogger LOG = new SampleAppLogger(
			MessageCreatorImpl.class);

	/**
	 * The cur message.
	 */
	private T curMessage;

	/**
	 * Gets the cur message.
	 * 
	 * @return the cur message
	 */
	public T getCurMessage() {
		return curMessage;
	}

	/**
	 * Sets the cur message.
	 * 
	 * @param curMessage
	 *            the new cur message
	 */
	public void setCurMessage(T curMessage) {
		this.curMessage = curMessage;
	}

	/* (non-Javadoc)
	 * @see org.springframework.jms.core.MessageCreator#createMessage(javax.jms.Session)
	 */
	@Override
	public Message createMessage(Session session) throws JMSException {
		Message message = session.createObjectMessage(curMessage);
		LOG.info("Sending message number [" + curMessage + "]");
		return message;
	}

}
